package com.xrit.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.constant.MessageConstant;
import com.xrit.constant.RedisMessageConstant;
import com.xrit.pojo.SetMeal;
import com.xrit.query.QueryPageBean;
import com.xrit.result.PageResult;
import com.xrit.result.Result;
import com.xrit.service.SetMealService;
import com.xrit.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("setMeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 图片上传
     * @return
     */
    @RequestMapping("/upload.do")
    public Result upload(MultipartFile imgFile){
        try{
            if (imgFile!=null && !imgFile.isEmpty()){
                //1.获取图片的原始名称   1.abc.jpg
                String originalFilename = imgFile.getOriginalFilename();

                //2.创建新的图片名称，保证图片名称的唯一性  khkhkhkhkhkhk.jpg
                String newName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));

                //3.上传到七牛云
                QiNiuUtils.uploadViaByte(imgFile.getBytes(),newName);

                //4.数据返回 {domain:"",imgName:""}
                Map<String,String> map=new HashMap<>();
                map.put("domain",QiNiuUtils.DOMAIN);//用户图片显示的外域
                map.put("imgName",newName);//图片名称

                //5.TODO：保存数据到redis中（所有的上传图片名称）
                jedisPool.getResource().sadd(RedisMessageConstant.SETMEAL_PIC_RESOURCES,newName);
                return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,map);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }



    /**
     * 添加套餐
     * @param setMeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/add.do")
    public Result add(@RequestBody SetMeal setMeal, Integer [] checkgroupIds){
        setMealService.addSetMeal(setMeal,checkgroupIds);

        //2.TODO 保存有效图片到redis中
        Jedis jedis = jedisPool.getResource();
        jedis.sadd(RedisMessageConstant.SETMEAL_PIC_DB_RESOURCES,setMeal.getImg());
        jedis.close();
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页条件查询
     */
    @RequestMapping("/findPage.do")
    public Result findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult all = setMealService.findPage(queryPageBean);
        return new Result(true, all==null? MessageConstant.QUERY_CHECKGROUP_FAIL: MessageConstant.QUERY_CHECKGROUP_SUCCESS,all);
    }
}
